<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "tebiedesbhaha");
$wechatObj = new wechatCallbackapiTest();
if($_GET["echostr"]){
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}



class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $type   = trim($postObj->MsgType);
                $mediaid = $postObj->MediaId;
                $time = time();
                $rplTpl = array(
                    'text'=>"<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>",
                    'image'=>'<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Image>
                            <MediaId><![CDATA[%s]]></MediaId>
                            </Image>
                            </xml>',
                    'pictext' => "<xml>
            				<ToUserName><![CDATA[%s]]></ToUserName>
        					<FromUserName><![CDATA[%s]]></FromUserName>
        					<CreateTime>%s</CreateTime>
        					<MsgType><![CDATA[%s]]></MsgType>
        					<ArticleCount>%d</ArticleCount>
        					<Articles>%s</Articles>
					</xml>",
		            'pictext_item' => "<item>
        					<Title><![CDATA[%s]]></Title> 
        					<Description><![CDATA[%s]]></Description>
        					<PicUrl><![CDATA[%s]]></PicUrl>
        					<Url><![CDATA[%s]]></Url>
        					</item>",
                    'music' =>"<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Music>
                            <Title><![CDATA[%s]]></Title>
                            <Description><![CDATA[%s]]></Description>
                            <MusicUrl><![CDATA[%s]]></MusicUrl>
                            <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
                            <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
                            </Music>
                            </xml>",
                    'voice' =>"<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Voice>
                            <MediaId><![CDATA[%s]]></MediaId>
                            </Voice>
                            </xml>",
                    );
                           
            if(!empty($mediaid)){
                $msgType ='image';
                $mediaid ='8pHuOz6rVhsy-RFDcnyX3ASmEdvaGdkUdxJSQFXZT-Cjmx6dP_1oDMkp_JOf0OvR';
                $resultStr = sprintf($rplTpl['image'],$fromUsername, $toUsername, $time, $msgType, $mediaid);
                echo $resultStr;
                
            }
                           
                 
                            
            if(!empty( $keyword ))
            {
                 if($keyword =='丁香花'){
                
                    $msgType ='music';
                    $ThumbMediaId ="UgbTAsFXM_a0b-tJ6I6VvxgJZKVjvuVPLYlHSFOtKBW4qVPa1PKKne1jiurZwwP_";
                    
                    
                    $resultStr = sprintf($rplTpl['music'],$fromUsername, $toUsername, $time, $msgType,'丁香花','丁香花','','',$ThumbMediaId);
                    echo $resultStr;
                    
                    
                }elseif($keyword =='音乐'){
                
                    $msgType ='voice';
                    $MediaId ="UgbTAsFXM_a0b-tJ6I6VvxgJZKVjvuVPLYlHSFOtKBW4qVPa1PKKne1jiurZwwP_";
                    
                    
                    $resultStr = sprintf($rplTpl['voice'],$fromUsername, $toUsername, $time, $msgType,$MediaId);
                    echo $resultStr;
                    
                    
                }else{     
                
                    $msgType = "text";
                	$contentStr = "youqianmeiyoua!";
                	$resultStr = sprintf($rplTpl['text'], $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                    
                }
            }else{
            	echo "Input something...";
            }

        }else {
        	echo "ssssssb";
        	exit;
        }
        
        
        
        
        
    }
		
	private function checkSignature()
	{
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>